<template>
  <div class="transaction-record">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg">
        <label>类型</label>
        <el-select v-model="searchForm.category" placeholder size="medium">
          <el-option v-for="(key,item) in categoryList" :key="key" :label="key" :value="item"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>币种</label>
        <el-select v-model="searchForm.currency" placeholder size="medium">
          <el-option
            v-for="item in currencyList"
            :key="item.coin_id"
            :label="item.currency"
            :value="item.currency"
          ></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>状态</label>
        <el-select v-model="searchForm.status" placeholder size="medium">
          <el-option v-for="(key,item) in statusList" :key="key" :label="key" :value="item"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>时间</label>
        <el-date-picker
          size="medium"
          v-model="searchForm.times"
          type="daterange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <br>
      <div class="item-msg">
        <label>UID/钱包地址</label>
        <el-input v-model="searchForm.uid_or_address" clearable size="medium" placeholder></el-input>
      </div>
      <el-button class="search-btn" @click="search()" size="medium" type="primary">搜索</el-button>
      <el-button class="search-btn" @click="reset()" size="medium">重置</el-button>
      <el-button class="search-btn" size="medium" @click="showDialog = true">导出</el-button>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="category_name" label="类型" width="120"></el-table-column>
        <el-table-column align="center" prop="from_address" label="转帐方" width="200">
          <template slot-scope="item">
            <p>
              {{item.row.from_relation_uid}}&nbsp;
              <span v-if="item.row.from_address != ''">|</span>
              &nbsp;{{item.row.from_address}}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="op_address" label="接收方" width="200">
          <template slot-scope="item">
            <p>
              {{item.row.to_relation_uid}}&nbsp;
              <span v-if="item.row.op_address != ''">|</span>
              &nbsp;{{item.row.op_address}}
            </p>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="currency" label="币种"></el-table-column>
        <el-table-column align="center" prop="amount" label="数量"></el-table-column>
        <!--        <el-table-column align="center" prop="" label="备注"></el-table-column>-->
        <el-table-column align="center" prop="create_at" width="165" label="完成时间"></el-table-column>
        <el-table-column align="center" prop="status_des" label="状态"></el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>
    </div>
    <!-- 导出excel -->
    <transition name="fade">
      <excelDialog
        v-if="showDialog"
        @cancel="showDialog=false"
        @success="showDialog=false"
        :fnc="back"
        :params="searchForm"
      ></excelDialog>
    </transition>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import excelDialog from "@/components/AssetsManage/excelDialog";
import {
  GetTransactionRecord,
  GetTransactionType,
  GetTransactionStatus
} from "./Assets.service";
import { supTokens } from "../FlashChange/FlashChange.service";

export default {
  components: {
    Pages,
    excelDialog
  },
  data() {
    return {
      showDialog: false,
      loading: false,
      searchForm: {
        category: "",
        status: "",
        currency: "",
        uid_or_address: "",
        page: 1,
        size: 10,
        start_time: null,
        end_time: null,
        times: []
      },
      categoryList: [],
      currencyList: [],
      statusList: [],
      tableData: [],
      total: 10
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.search();
      let res = await GetTransactionType();
      this.categoryList = res.data || [];
      let res1 = await GetTransactionStatus();
      this.statusList = res1.data || [];
      let res2 = await supTokens();
      this.currencyList = res2.data || [];
    },
    async search() {
      this.loading = true;
      if (this.searchForm.times && this.searchForm.times.length > 0) {
        this.searchForm.start_time = this.searchForm.times[0];
        this.searchForm.end_time = this.searchForm.times[1];
      } else {
        this.searchForm.start_time = this.searchForm.end_time = null;
      }
      const res = await GetTransactionRecord(this.searchForm);
      this.tableData = res.data ? res.data.data : [];
      this.total = res.data ? Number(res.data.total) : 0;
      this.loading = false;
    },
    reset() {
      this.searchForm = {
        category: "",
        status: "",
        currency: "",
        uid_or_address: "",
        page: 1,
        size: 10,
        start_time: "",
        end_time: ""
      };
    },
    back(params) {
      return GetTransactionRecord(params);
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(val);
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.searchForm.page = 1;
      this.search();
    }
  }
};
</script>

<style lang="scss" scoped>
.transaction-record {
  position: relative;
  min-height: calc(100vh - 100px);

  .search-block {
    width: 100%;
    padding: 25px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;

    .item-msg {
      display: inline-block;
      margin-right: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;

      label {
        display: inline-block;
        min-width: 80px;
        text-align: right;
        margin-right: 10px;
      }

      .el-input {
        width: 150px;
      }

      .el-select {
        width: 150px;
      }

      .el-date-editor {
        width: 270px;
      }
    }

    .search-btn {
      margin-left: 30px;
    }

    .search-btn:nth-of-type(1) {
      margin-left: 50px;
    }
  }

  .data-table {
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
  }
}
</style>
